package it.polimi.entitiesManagers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import it.polimi.authentication.ejb.usermanagement.Group;
import it.polimi.entities.Hotel;
import it.polimi.entitiesDTO.HotelDTO;

@Stateless
public class HotelManagerBean implements HotelMgr {

	@PersistenceContext
	private EntityManager em;
	
	@Resource
	private EJBContext context;
	
	@Override
	@RolesAllowed({Group._EMPLOYEE})
	public void save(HotelDTO hotel) {
		Hotel newHotel = new Hotel(hotel);
		em.persist(newHotel);
	}

	@Override
	public void update(HotelDTO hotel) {
		em.merge(new Hotel(hotel));
	}

	@Override
	public HotelDTO getHotelDTO() {
		HotelDTO hotelDTO = convertToDTO(getPrincipalHotel());
		return hotelDTO;
	}
	
	public Hotel find(String name) {
    	return em.find(Hotel.class, name);
    }
	
	public Hotel getPrincipalHotel() {
    	return find(getPrincipalName());
    }
	
	public String getPrincipalName() {
    	return context.getCallerPrincipal().getName();
    }
	
	public Hotel getHotel(String name){
		return em.createNamedQuery(Hotel.FIND_HOTEL, Hotel.class).setParameter("name", name).getSingleResult();
	}
	
	public Hotel getHotel(int id){
		return em.createNamedQuery(Hotel.FIND_BY_ID, Hotel.class).setParameter("id", id).getSingleResult();
	}

    
private HotelDTO convertToDTO(Hotel hotel) {
		HotelDTO hotelDTO = new HotelDTO();
		hotelDTO.setName(hotel.getName());
		hotelDTO.setCity(hotel.getCity());
		hotelDTO.setStars(hotel.getStars());
		hotelDTO.setOpenDate(hotel.getOpenDate());
		hotelDTO.setClosureDate(hotel.getClosureDate());
		hotelDTO.setPrice(hotel.getPrice());
		hotelDTO.setSpots(hotel.getSpots());
		return hotelDTO;
	}

private List<Hotel> hotelByCity(String city){
	return em.createNamedQuery(Hotel.FIND_BY_CITY, Hotel.class).setParameter("city", city).getResultList();
}
@Override
@RolesAllowed({Group._EMPLOYEE})
public List<String> getHotelByCity(String city) {
	List<String> someHotel= new ArrayList<String>();
	for(Hotel h: hotelByCity(city))
		someHotel.add(h.toString());
	return someHotel;
}

@Override
public List<Integer> getAllId() {
	List<Integer> list = new ArrayList<Integer>();
	List<Hotel> hotels = em.createNamedQuery(Hotel.FIND_ALL, Hotel.class).getResultList();
    for(Hotel h: hotels){
    	list.add((Integer)h.getId());
    }
    return list;
}

@Override
public List<String> showAllHotels() {
	List<String> list = new ArrayList<String>();
	List<Hotel> hotels = em.createNamedQuery(Hotel.FIND_ALL, Hotel.class).getResultList();
	for ( Hotel h: hotels){
		if(h.getVisibility()){
			list.add(h.toString());
		}
	}
	return list;
}

@Override
public void remove(int hotelId) {
	Hotel hotel = em.createNamedQuery(Hotel.FIND_BY_ID, Hotel.class).setParameter("id", hotelId).getSingleResult();
	em.remove(hotel);
}

}
